/*
 * @FilePath: LayerManager.js
 * @Author: jzb
 * @Date: 2023-11-06 11:29:58
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-11-07 18:20:59
 * @Descripttion: 
 */


import TileLayer from "ol/layer/Tile";
import OSM from "ol/source/OSM";
import XYZ from "ol/source/XYZ";
import WMS from "ol/source/TileWMS";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import GeoJSON from "ol/format/GeoJSON";
export default class LayerManager {

	baseLayers;
	map;
	layers;

	// constructor(layerinfo) {
	//     this.baseLayers = []
	//     this.init(layerinfo)
	// }
	constructor(map) {
		this.map = map
	}
	setMap(map) {
		this.map = map
	}
	initBaseLayer(layerinfo) {
		this.baseLayers = []
		let layer;
		switch (layerinfo.type) {
			case "osm":
				layer = this.initOSMLayer()
				break
			case "xyz":
				layer = this.initXYZLayer(layerinfo)
				break
			case "wms":
				layer = this.initWMSLayer(layerinfo)
				break
				
			default:
				layer = this.initOSMLayer()
				break

		}
		this.baseLayers.push(layer)
	}

	initOSMLayer() {
		return new TileLayer({
			source: new OSM()
		})
	}
	initXYZLayer(layerinfo) {
		return new TileLayer({
			title: layerinfo.title,
			source: new XYZ({
				url: layerinfo.url
			})

		})
	}
	initWMSLayer(layerinfo) {
		return new TileLayer({
			title: layerinfo.title,
			source: new WMS({
				url: layerinfo.url,
				params: {
					'LAYERS': layerinfo.layers,
					'TILED': true
				}
			})
		})
	}

	initFeatureLayer(layerinfo) {
		return new VectorLayer({
			title: layerinfo.title,
			// source: new VectorSource({
			//    features:new GeoJSON().readFeature(layerinfo.features)
			// }),
			source: new VectorSource({
				url: layerinfo.url,
				format: new GeoJSON()
			}),
			style: layerinfo.renderPoint
		})
	}
	addLayers(arrs) {
		if (!this.layers) {
			this.layers = {}
		}

		arrs.forEach(d => {
			let source = new VectorSource({features:[]})
			let layer = new VectorLayer({
				source: source,
				className: source

			})
			this.map.addLayer(layer)
			this.layers[d] = layer
		})

	}
	clearLayer(layerName) {
		if (!this.layers && !this.layers[layerName]) {
			return
		}
		let source = this.layers[layerName].getSource
		source.clear()
	}



}